package com.inspur.dgov.policy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inspur.dgov.common.secure.dto.SecureUser;
import com.inspur.dgov.common.secure.utils.SecureUtil;
import com.inspur.dgov.common.tool.api.R;
import com.inspur.dgov.common.tool.constant.AppConstant;
import com.inspur.dgov.common.tool.utils.UniqueIdGenerator;
import com.inspur.dgov.policy.entity.PolicyCollectInfo;
import com.inspur.dgov.policy.service.PolicyCollectInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 政策采集清单信息 前端控制器
 * </p>
 *
 * @author chiyupei
 * @since 2020-04-30
 */
@RestController
@RequestMapping("/collect")
@Api(value = "政策采集清单", tags = "政策采集清单")
@Slf4j
public class PolicyCollectInfoController {

    @Autowired
    private PolicyCollectInfoService policyCollectInfoService;

    /**
     * 政策采集清单列表
     */
    @PostMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collectType", value = "采集分类", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "regionCode", value = "区划简码", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "regionLevel", value = "区划范围", paramType = "query", dataType = "string")
    })
    @ApiOperation(value = "政策采集清单列表", notes = "传入采集分类或所属区划", position = 1)
    public R<IPage<PolicyCollectInfo>> page(@ApiIgnore @RequestParam Map<String, Object> query, Page page) {
        //判断查询条件中区划范围
        Object region = query.get("regionCode");
        if (null != region) {
            String regionCode = region.toString();
            String level = query.get("regionLevel") + "";
            if (AppConstant.DB_STATUS_NORMAL.equals(level)) {
                regionCode = regionCode.replaceAll("0+$", "");
                query.put("regionCode", regionCode);
            }
        }
        //TODO 根据当前用户所属租户过滤数据

        IPage<PolicyCollectInfo> pages = this.policyCollectInfoService.selectPolicyCollectInfoPage(page, query);
        return R.data(pages);
    }

    /**
     * 新增政策采集清单
     *
     * @param collection
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增政策采集清单", notes = "传入政策采集清单信息", position = 2)
    public R add(@RequestBody PolicyCollectInfo collection) {
        SecureUser curUser = SecureUtil.getUser();
        LocalDateTime now = LocalDateTime.now();
        Long id = UniqueIdGenerator.generateId();
        collection.setId(id);
        collection.setCreator(Long.valueOf(curUser.getUserId()));
        collection.setCreateTime(now);
        collection.setStatus(AppConstant.DB_STATUS_NORMAL);
        collection.setTenantId(Long.valueOf(curUser.getTenantId()));
        boolean ret = this.policyCollectInfoService.save(collection);
        return R.status(ret);
    }

    /**
     * 更新政策采集清单
     *
     * @param collection
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新政策采集清单", notes = "传入政策采集清单信息", position = 3)
    public R update(@RequestBody PolicyCollectInfo collection) {
        SecureUser curUser = SecureUtil.getUser();
        LocalDateTime now = LocalDateTime.now();
        collection.setUpdator(Long.valueOf(curUser.getTenantId()));
        collection.setUpdateTime(now);
        boolean ret = this.policyCollectInfoService.updateById(collection);
        return R.status(ret);
    }

    /**
     * 删除政策采集清单
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除政策采集清单", notes = "传入政策采集清单信息", position = 4)
    public R remove(@RequestBody PolicyCollectInfo collection) {
        return R.status(this.policyCollectInfoService.removeById(collection));
    }
}
